package top.luoqiz.jenkins.config.security;

import lombok.SneakyThrows;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author luoqiz
 */
@Configuration
public class AuthenticationHandler {

    @Configuration
    static class SysAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().println("登录成功");
            System.out.printf("登录成功");
        }
    }

    @Configuration
    static class SysAuthenticationFailureHandler implements AuthenticationFailureHandler {


        @Override
        public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().println("登录失败");
            System.out.printf("登录失败");
        }
    }

    @Configuration
    static class SysLogoutSuccessHandler implements LogoutSuccessHandler {

        @Override
        public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().println("LogoutSuccessHandler 注销成功");
            System.out.printf("LogoutSuccessHandler 注销成功");
        }
    }

    @Configuration
    static class SysLogoutHandler implements LogoutHandler {

        @SneakyThrows
        @Override
        public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().println("SysLogoutHandler 注销成功");
            System.out.printf("SysLogoutHandler 注销成功");
        }
    }
}
